import { Service , Inject } from "typedi";
import {TagEntity} from '../entity/TagEntity'
import {TagClassEntity} from '../entity/TagClassEntity'
import { getRepository,getConnection } from "typeorm";
import {
    getIndexTpoic
  } from "../model/TopicModel";
@Service()
export class TopicDao{

    public async getOneTopicDao(topic_Id:number): Promise<any> {

        return await   getRepository(TagEntity)
                            .createQueryBuilder("tag")
                            .where("id = :id", { id: topic_Id })
                            .getOne();
    }


    public async getAllTagClass(): Promise<any> {

        return await   getRepository(TagClassEntity)
                            .createQueryBuilder("tagClass")
                            .getMany();
    }
    
    public async changeSubTopic(topic_Id:number,isSub:string): Promise<any> {
        return await   getConnection()
        .createQueryBuilder()
        .update(TagEntity)
        .where("id = :id", { id: topic_Id })
        .set({ tag_SubNum:()=>`tag_SubNum  ${isSub == 'true'?'+':'-'} 1`})
        .execute();
    }


    public async getHotTopic(): Promise<any> {

        return await   getRepository(TagEntity)
                            .createQueryBuilder("tag")
                            .where("tag_Status = :index", { index: 3 })
                            .orderBy("tag.tag_SubNum", "DESC")
                            .getMany();
    }

    public async getIndexAllTpoic(params:getIndexTpoic): Promise<any> {

        return await   getRepository(TagEntity)
                            .createQueryBuilder("tag")
                            .where(`tag_ClassIndex ${params.topic_Index == 2?'<>':'='} :tag_ClassIndex`, { tag_ClassIndex: params.topic_Index})
                            .orderBy("tag.tag_SubNum", "DESC")
                            .skip(params.topic_Num*params.topic_Page)
                            .take(params.topic_Num)
                            .getMany();
    }
    
}